import 'package:finance_app/src/entities/invoice_entity.dart';
import 'package:finance_app/src/entities/business_contact_entity.dart';
import 'package:finance_app/src/entities/invoice_detail_entity.dart';

invoiceEntityFromJson(InvoiceEntity data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['contact_id'] != null) {
		data.contactId = json['contact_id'] is String
				? int.tryParse(json['contact_id'])
				: json['contact_id'].toInt();
	}
	if (json['quotation_id'] != null) {
		data.quotationId = json['quotation_id'] is String
				? int.tryParse(json['quotation_id'])
				: json['quotation_id'].toInt();
	}
	if (json['number'] != null) {
		data.number = json['number'].toString();
	}
	if (json['quotation_number'] != null) {
		data.quotationNumber = json['quotation_number'].toString();
	}
	if (json['issued_at'] != null) {
		data.issuedAt = json['issued_at'].toString();
	}
	if (json['delivery_at'] != null) {
		data.deliveryAt = json['delivery_at'].toString();
	}
	if (json['due_at'] != null) {
		data.dueAt = json['due_at'].toString();
	}
	if (json['currency_code'] != null) {
		data.currencyCode = json['currency_code'].toString();
	}
	if (json['subtotal'] != null) {
		data.subtotal = json['subtotal'] is String
				? num.tryParse(json['subtotal'])
				: json['subtotal'];
	}
	if (json['exclusive_tax_amount'] != null) {
		data.exclusiveTaxAmount = json['exclusive_tax_amount'] is String
				? num.tryParse(json['exclusive_tax_amount'])
				: json['exclusive_tax_amount'];
	}
	if (json['inclusive_tax_amount'] != null) {
		data.inclusiveTaxAmount = json['inclusive_tax_amount'] is String
				? num.tryParse(json['inclusive_tax_amount'])
				: json['inclusive_tax_amount'];
	}
	if (json['discount_type'] != null) {
		data.discountType = json['discount_type'].toString();
	}
	if (json['discount_amount'] != null) {
		data.discountAmount = json['discount_amount'] is String
				? num.tryParse(json['discount_amount'])
				: json['discount_amount'];
	}
	if (json['total'] != null) {
		data.total = json['total'] is String
				? num.tryParse(json['total'])
				: json['total'];
	}
	if (json['notes'] != null) {
		data.notes = json['notes'];
	}
	if (json['status'] != null) {
		data.status = json['status'].toString();
	}
	if (json['created_by'] != null) {
		data.createdBy = json['created_by'];
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['items'] != null) {
		data.items = (json['items'] as List).map((v) => InvoiceDetailItems().fromJson(v)).toList();
	}
	if (json['contact'] != null) {
		data.contact = BusinessContactEntity().fromJson(json['contact']);
	}
	if (json['unpaid'] != null) {
		data.unpaid = json['unpaid'] is String
				? num.tryParse(json['unpaid'])
				: json['unpaid'];
	}
	if (json['is_overdue'] != null) {
		data.isOverdue = json['is_overdue'] is String
				? int.tryParse(json['is_overdue'])
				: json['is_overdue'].toInt();
	}
	return data;
}

Map<String, dynamic> invoiceEntityToJson(InvoiceEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['contact_id'] = entity.contactId;
	data['quotation_id'] = entity.quotationId;
	data['number'] = entity.number;
	data['quotation_number'] = entity.quotationNumber;
	data['issued_at'] = entity.issuedAt;
	data['delivery_at'] = entity.deliveryAt;
	data['due_at'] = entity.dueAt;
	data['currency_code'] = entity.currencyCode;
	data['subtotal'] = entity.subtotal;
	data['exclusive_tax_amount'] = entity.exclusiveTaxAmount;
	data['inclusive_tax_amount'] = entity.inclusiveTaxAmount;
	data['discount_type'] = entity.discountType;
	data['discount_amount'] = entity.discountAmount;
	data['total'] = entity.total;
	data['notes'] = entity.notes;
	data['status'] = entity.status;
	data['created_by'] = entity.createdBy;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['items'] =  entity.items?.map((v) => v.toJson())?.toList();
	data['contact'] = entity.contact?.toJson();
	data['unpaid'] = entity.unpaid;
	data['is_overdue'] = entity.isOverdue;
	return data;
}

invoiceItemsFromJson(InvoiceItems data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['invoice_id'] != null) {
		data.invoiceId = json['invoice_id'] is String
				? int.tryParse(json['invoice_id'])
				: json['invoice_id'].toInt();
	}
	if (json['type'] != null) {
		data.type = json['type'].toString();
	}
	if (json['type_id'] != null) {
		data.typeId = json['type_id'] is String
				? int.tryParse(json['type_id'])
				: json['type_id'].toInt();
	}
	if (json['name'] != null) {
		data.name = json['name'].toString();
	}
	if (json['quantity'] != null) {
		data.quantity = json['quantity'] is String
				? int.tryParse(json['quantity'])
				: json['quantity'].toInt();
	}
	if (json['currency_code'] != null) {
		data.currencyCode = json['currency_code'].toString();
	}
	if (json['tax_id'] != null) {
		data.taxId = json['tax_id'] is String
				? int.tryParse(json['tax_id'])
				: json['tax_id'].toInt();
	}
	if (json['price'] != null) {
		data.price = json['price'] is String
				? num.tryParse(json['price'])
				: json['price'];
	}
	if (json['nett_price'] != null) {
		data.nettPrice = json['nett_price'] is String
				? num.tryParse(json['nett_price'])
				: json['nett_price'];
	}
	if (json['is_tax_inclusive'] != null) {
		data.isTaxInclusive = json['is_tax_inclusive'] is String
				? int.tryParse(json['is_tax_inclusive'])
				: json['is_tax_inclusive'].toInt();
	}
	if (json['tax_amount'] != null) {
		data.taxAmount = json['tax_amount'] is String
				? num.tryParse(json['tax_amount'])
				: json['tax_amount'];
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	return data;
}

Map<String, dynamic> invoiceItemsToJson(InvoiceItems entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['invoice_id'] = entity.invoiceId;
	data['type'] = entity.type;
	data['type_id'] = entity.typeId;
	data['name'] = entity.name;
	data['quantity'] = entity.quantity;
	data['currency_code'] = entity.currencyCode;
	data['tax_id'] = entity.taxId;
	data['price'] = entity.price;
	data['nett_price'] = entity.nettPrice;
	data['is_tax_inclusive'] = entity.isTaxInclusive;
	data['tax_amount'] = entity.taxAmount;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	return data;
}